const MongoClient = require('mongodb').MongoClient;

const uri = 'mongodb://localhost:27017'; // MongoDB 连接字符串
const dbName = 'your_database_name'; // 数据库名称

async function getMappedData() {
  try {
    const client = await MongoClient.connect(uri);
    const db = client.db(dbName);

    // 获取 video-user 映射表的数据
    const mappingCollection = db.collection('video_user_mapping');
    const mappingData = await mappingCollection.find().toArray();

    // 获取 video 表的数据
    const videoCollection = db.collection('video');
    const videoData = await videoCollection.find().toArray();

    // 获取 user 表的数据
    const userCollection = db.collection('user');
    const userData = await userCollection.find().toArray();

    // 进行映射并生成最终数据
    const mappedData = mappingData.map((item) => {
      const video = videoData.find((v) => v.id === item.videoId);
      const user = userData.find((u) => u.id === item.userId);
      return {
        id: item.id,
        video: video,
        user: user
      };
    });

    // 输出最终数据
    console.log(mappedData);

    client.close();
  } catch (error) {
    console.error('An error occurred:', error);
  }
}

getMappedData();